//
//  MYStatusToolBar.swift
//  MYWeibo
//
//  Created by Magic Yu on 16/4/17.
//  Copyright © 2016年 Magic Yu. All rights reserved.
//

import UIKit

class MYStatusToolBar: UIView {
    
    var statusViewModel: MYStatusViewModel? {
        didSet{
            retweetBtn.setTitle(statusViewModel?.rewteetTitle, forState: .Normal)
            commentBtn.setTitle(statusViewModel?.commentTitle, forState: .Normal)
            praiseBtn.setTitle(statusViewModel?.praiseTitle, forState: .Normal)
        }
    }
    
    //声明三个按钮 确保按钮一定有值 不管是在哪里赋值 必选项
    var  retweetBtn: UIButton!
    var commentBtn: UIButton!
    var praiseBtn: UIButton!

    override init(frame: CGRect) {
        super.init(frame: frame)
        
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    //MARK: 设置UI界面
    private func setupUI() {
        
        //添加按钮
        retweetBtn = addChildBtn("转发", imageName: "timeline_icon_retweet")
        commentBtn = addChildBtn("评论", imageName: "timeline_icon_comment")
        praiseBtn = addChildBtn("赞", imageName: "timeline_icon_like")
        
        
        //添加约束 三等分视图
        //设置三个按钮的宽度相等 并且最后一个按钮的右边约束与self相等 这样按钮就平铺开了
        retweetBtn.snp_makeConstraints(closure: { (make) -> Void in
            make.left.top.bottom.equalTo(self)
        })
        
        commentBtn.snp_makeConstraints(closure: { (make) -> Void in
            make.left.equalTo(retweetBtn.snp_right)
            make.top.bottom.equalTo(self)
            make.width.equalTo(retweetBtn)
        })
        
        praiseBtn.snp_makeConstraints(closure: { (make) -> Void in
            make.left.equalTo(commentBtn.snp_right)
            make.top.bottom.right.equalTo(self)
            make.width.equalTo(commentBtn)
        })
        
        //创建分割线
        let sep1 = sepLine()
        let sep2 = sepLine()
        
        //设置分割线的宽度和高度
        //宽度由屏幕分辨率确定
        let w = 1 / UIScreen.mainScreen().scale
        //高度黄金分割 
        let multi = 0.4
        
        //分割线设置约束
        sep1.snp_makeConstraints { (make) -> Void in
            make.left.equalTo(retweetBtn.snp_right)
            make.width.equalTo(w)
            make.height.equalTo(self.snp_height).multipliedBy(multi)
            make.centerY.equalTo(self)
            
        }
        
        sep2.snp_makeConstraints { (make) -> Void in
            make.left.equalTo(commentBtn.snp_right)
            make.width.equalTo(w)
            make.height.equalTo(self.snp_height).multipliedBy(multi)
            make.centerY.equalTo(self)

        }
        
    }
    
    //创建分割线
    private func sepLine() -> UIView {
    
        let line = UIView()
        line.backgroundColor = UIColor.orangeColor()
        addSubview(line)
        return line
        
    }
    
//    //MARK: 懒加载子视图
//    private lazy var 
    //因为有三个按钮 懒加载的话需要加载三次 所以用方法
    private func addChildBtn(title: String, imageName: String) -> UIButton {
        let btn = UIButton()
        
        btn.setTitle(title, forState: .Normal)
        btn.setTitleColor(UIColor.darkGrayColor(), forState: .Normal)
        btn.titleLabel?.font = UIFont.systemFontOfSize(12)
        btn.setImage(UIImage(named: imageName), forState: .Normal)
        btn.setBackgroundImage(UIImage(named: "timeline_card_bottom_background"), forState: .Normal)
        btn.setBackgroundImage(UIImage(named: "timeline_card_bottom_background_highlighted"), forState: .Highlighted)
        
        btn.sizeToFit()
        addSubview(btn)
        return btn
    }
 
}
